package com.fenghuajueli.idiomppp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.entity.VideoInfoEntity;
import com.fenghuajueli.idiomppp.utils.DateUtils;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.utils.ImageLoadManager;

import java.util.List;

/**
 * 推荐数据适配器
 */
public class RecommendGridAdapter extends BaseAdapter {

    private List<VideoInfoEntity> videoInfoEntities;
    private Context mContext;
    private LayoutInflater mInflater;
    private int itemWidth;
    private OnListClickListener<VideoInfoEntity> onListClickListener;


    public RecommendGridAdapter(Context context, List<VideoInfoEntity> videoInfoEntities) {
        this.videoInfoEntities = videoInfoEntities;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        itemWidth = (ScreenUtils.getScreenWidth() - ConvertUtils.dp2px(67)) / 2;
    }

    public void refreshData(List<VideoInfoEntity> videoInfoEntities) {
        this.videoInfoEntities = videoInfoEntities;
        notifyDataSetChanged();
    }

    public void setOnListClickListener(OnListClickListener<VideoInfoEntity> onListClickListener) {
        this.onListClickListener = onListClickListener;
    }

    @Override
    public int getCount() {
        return videoInfoEntities.size();
    }

    @Override
    public Object getItem(int i) {
        return videoInfoEntities.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.recommend_grid_item_layout, null);
            holder.tvIdiomName = convertView.findViewById(R.id.tvIdiomName);
            holder.storyImg = convertView.findViewById(R.id.storyImg);
            holder.tvCategory = convertView.findViewById(R.id.tvCategory);
            holder.itemContainer = convertView.findViewById(R.id.itemContainer);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(itemWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
            convertView.setLayoutParams(layoutParams);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        VideoInfoEntity videoInfoEntity = videoInfoEntities.get(position);
        if (position % 4 == 0) {
            ImageLoadManager.loadForImageView(holder.storyImg, R.mipmap.chengyu_img1);
        } else if (position % 4 == 1) {
            ImageLoadManager.loadForImageView(holder.storyImg, R.mipmap.chengyu_img2);
        } else if (position % 4 == 2) {
            ImageLoadManager.loadForImageView(holder.storyImg, R.mipmap.chengyu_img3);
        } else if (position % 4 == 3) {
            ImageLoadManager.loadForImageView(holder.storyImg, R.mipmap.chengyu_img4);
        } else {
            ImageLoadManager.loadForImageView(holder.storyImg, R.mipmap.chengyu_img1);
        }
        holder.tvIdiomName.setText(videoInfoEntity.getDesc());
        holder.itemContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (onListClickListener != null) {
                    onListClickListener.itemClick(position, videoInfoEntity);
                }
            }
        });
        int showCount = position * 20 + DateUtils.getDayInYear() + 250;
        holder.tvCategory.setText(showCount+"人浏览 | 成语故事");
        return convertView;
    }

    final class ViewHolder {
        TextView tvIdiomName;
        ImageView storyImg;
        TextView tvCategory;
        LinearLayout itemContainer;
    }

}
